//
//  QSFTabBarController.m
//  QSF_Dev
//
//  Created by Mr.Dynasty on 2018/6/5.
//  Copyright © 2018年 Mr.Dynasty. All rights reserved.
//

#import "TabBarController.h"
#import "LoginViewController.h"
#import "HomeViewController.h"
#import "MineViewController.h"
#import "RecordViewController.h"
#import "SetViewController.h"

@interface TabBarController ()

@end

@implementation TabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self customTabbar];
    [self createChildViewControllers];
//    [self checkVersion];

}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if (![UserManager shareManager].userInfoModel.isLogin) {
        [self showLogin];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)customTabbar {
    //去掉tabBar顶部黑色线条
    CGRect rect = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    CGContextFillRect(context, rect);
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [self.tabBar setBackgroundImage:img];
    [self.tabBar setShadowImage:img];
    self.tabBar.backgroundColor = [UIColor whiteColor];
    //自定义一根线条
    UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
    topLine.backgroundColor = DefaultBackColor;
    [self.tabBar addSubview:topLine];
}

- (void)createChildViewControllers {
    HomeViewController *productVC = [[HomeViewController alloc] init];
    [self setUpOneChildVcWithVc:productVC Image:@"icon_shouye" selectedImage:@"icon_shouye_have" title:@"Home"];
    
    RecordViewController *recordVC = [[RecordViewController alloc] init];
    [self setUpOneChildVcWithVc:recordVC Image:@"icon_jilu" selectedImage:@"icon_jilu_have" title:@"Record" ];
    
    SetViewController *setVC = [[SetViewController alloc] init];
    [self setUpOneChildVcWithVc:setVC Image:@"icon_shezhi" selectedImage:@"icon_have_shezhi" title:@"Set"];
    

}

- (void)changeChildViewControllersWithRole:(NSInteger)role {
    
}

#pragma mark - 初始化设置tabBar上面单个按钮的方法

/**
 *  @author li bo, 16/05/10
 *
 *  设置单个tabBarButton
 *
 *  @param Vc            每一个按钮对应的控制器
 *  @param image         每一个按钮对应的普通状态下图片
 *  @param selectedImage 每一个按钮对应的选中状态下的图片
 *  @param title         每一个按钮对应的标题
 */
- (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title
{
    BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:Vc];
    
    UIImage *myImage = [UIImage imageNamed:image];
    myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //tabBarItem，是系统提供模型，专门负责tabbar上按钮的文字以及图片展示
    Vc.tabBarItem.image = myImage;
    UIImage *mySelectedImage = [UIImage imageNamed:selectedImage];
    mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    Vc.tabBarItem.selectedImage = mySelectedImage;
    Vc.tabBarItem.title = title;
    Vc.navigationItem.title = title;
    NSDictionary *dictHome = [NSDictionary dictionaryWithObject:HEX_COLOR(0xF7CE47) forKey:NSForegroundColorAttributeName];
    [Vc.tabBarItem setTitleTextAttributes:dictHome forState:UIControlStateSelected];
    NSDictionary *normalColorDi = [NSDictionary dictionaryWithObject:TextBlackColor forKey:NSForegroundColorAttributeName];
    [Vc.tabBarItem setTitleTextAttributes:normalColorDi forState:UIControlStateNormal];
    [self addChildViewController:nav];
}

- (void)showLogin {
    
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    BaseNavigationController *loginNav = [[BaseNavigationController alloc] initWithRootViewController:loginVC];
    [self presentViewController:loginNav animated:YES completion:nil];
}

//- (void)checkVersion {
//    
//    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
//    // 获取App的版本号
//    NSString *appVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
//    // 获取App的build版本
//    NSInteger appBuildVersion = [[infoDic objectForKey:@"CFBundleVersion"] integerValue];
//    
//    [[QSFNetManager shareManager] getWithUrl:GetApi(Version) params:nil success:^(id data) {
//        NSDictionary *parmaDic = (NSDictionary *)data;
//        QSFVersion *version = [QSFVersion yy_modelWithDictionary:parmaDic];
//        NSInteger serviceVersion = [version.version integerValue];
//        if (appBuildVersion < serviceVersion) {
//            [self showVersionInfo];
//        }
//    } failure:^(NSString *error) {
//        
//    }];
//}
//
//- (void)showVersionInfo {
//    UIAlertView *view=[[UIAlertView alloc] initWithTitle:@"版本更新"
//                                                 message:@"七色纺有新版本啦，快去用用看"
//                                                delegate:nil
//                                       cancelButtonTitle:@"不必了"
//                                       otherButtonTitles:@"立即更新", nil];
//    [view handlerClickedButton:^(NSInteger btnIndex) {
//        if (btnIndex==1) {
//            NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id1414133816"];
//            [[UIApplication sharedApplication] openURL:url];
//        }
//    }];
//    
//    [view show];
//}


@end
